//
//  ShowFailScreen.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/22.
//
//

#include "ShowFailScreen.h"
#include "MapMenuItem.h"
#include "MapScreen.h"
#include "TitleMenuItem.h"
#include "PlayLevelRecorder.h"
#include "LevelScreen.h"
#include "LocalizedStringGetter.h"
#include "CountDownModeLevelScreen.h"
#define BUTTON_WIDTH 135
#define BUTTON_HEIGHT 50
#define BUTTON_Y      200
#define BACK_WIDTH    50
#define TITLEY        300
#define TITLE_COLOR   Color3B::RED //Color3B::Color3B(107, 36, 120)
#define TITLE_FONT_SIZE  46
#define TITLE_FONT_NAME  "arial"
#define FAIL_FONT_SIZE  20
#define FAIL_FONT_NAME  "arial"
#define FAIL_COLOR    Color3B::RED//Color3B::Color3B(28, 52, 219)
#define FAIL_Y        400

typedef enum {
    ZBg = 0,
    ZMenu,
    
} Zindex;

typedef enum {
    TagMenu = 1,
    TagReplay,
    TagNext,
    TagBack,
    
    
} MenuTag;

ShowFailScreen::ShowFailScreen()
{
}



Scene *ShowFailScreen::createScene()
{
    auto scene = Scene::create();
    auto layer = ShowFailScreen::create();
    scene->addChild(layer);
    return scene;
}

bool ShowFailScreen::init()
{
    if (!Layer::init()) {
        return false;
    }
    bgInit();
    titleInit();
    failInit();
    menuInit();
    
    return true;
}

void ShowFailScreen::bgInit()
{
    auto bg = Sprite::create("WinBg.png");
    bg->setAnchorPoint(Vec2::ZERO);
    bg->setPosition(Vec2::ZERO);
    this->addChild(bg,ZBg);
}

void ShowFailScreen::failInit()
{
    Size winSize = Director::getInstance()->getWinSize();
    LabelTTF *fail = LabelTTF::create(LocalizedStringGetter::getStringByKey("Fail"),FAIL_FONT_NAME , FAIL_FONT_SIZE);
    fail->setAnchorPoint(Vec2::ZERO);
    fail->setPosition((winSize.width-fail->getContentSize().width)/2, FAIL_Y);
    fail->setColor(FAIL_COLOR);
    addChild(fail);
}

void ShowFailScreen::titleInit()
{
    Size winSize = Director::getInstance()->getWinSize();
    __String *titleStr = __String::createWithFormat(LocalizedStringGetter::getStringByKey("WinTitle"),PlayLevelRecorder::getInstance()->getNowPlayLevel());
    LabelTTF *title = LabelTTF::create(titleStr->getCString(),TITLE_FONT_NAME , TITLE_FONT_SIZE);
    title->setAnchorPoint(Vec2::ZERO);
    title->setPosition((winSize.width-title->getContentSize().width)/2, TITLEY);
    title->setColor(TITLE_COLOR);
    addChild(title);
}

void ShowFailScreen::menuInit()
{
    Size winsize = Director::getInstance()->getWinSize();
    auto itemReplay = TitleMenuItem::create("WinButtonBg.png", "WinButtonSelected.png","WinButtonBg.png", CC_CALLBACK_1(ShowFailScreen::menuCallback, this));
    itemReplay->setAnchorPoint(Vec2::ZERO);
    itemReplay->setTag(TagReplay);
    itemReplay->setTitle(LocalizedStringGetter::getStringByKey("RePlay"), Size(BUTTON_WIDTH,BUTTON_HEIGHT));
    itemReplay->setPosition(Point((winsize.width-BUTTON_WIDTH)/2,BUTTON_Y));
    
    auto itemBack = TitleMenuItem::create("WinBackButton.png", "WinBackButtonSelected.png","WinBackButton.png", CC_CALLBACK_1(ShowFailScreen::menuCallback, this));
    itemBack->setAnchorPoint(Vec2::ZERO);
    itemBack->setTag(TagBack);
    itemBack->setPosition(Point(winsize.width-BACK_WIDTH,winsize.height-BACK_WIDTH));
    
    auto menu = Menu::create(itemReplay,itemBack, NULL);
    menu->setAnchorPoint(Vec2::ZERO);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu);
}

void ShowFailScreen::menuCallback(Ref *sender)
{
    TitleMenuItem *item = (TitleMenuItem*)sender;
    if(item->getTag() == TagBack){
        Director::getInstance()->popScene();
    }else if(item->getTag() == TagReplay){
        rePlayDo();
    }

}

void ShowFailScreen::rePlayDo()
{
	int level = PlayLevelRecorder::getInstance()->getNowPlayLevel();
	if(level < LevelScreenTypeTypeMax){
    Director::getInstance()->replaceScene(LevelScreen::createScene(level));
	}else if(level > CountDownModeLevelScreenTypeNone){
	Director::getInstance()->replaceScene(CountDownModeLevelScreen::createScene(level));
	}
}
